package org.apache.james.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.james.model.CustomUser;

import java.util.List;

/**
 * 用户映射
 * @author ccoke
 */
public interface CustomUserMapper {

    /**
     * 根据用户名获取用户
     */
    @Select("SELECT CONCAT(a.username, '@', a.email_domain) as username, a.password AS password FROM (SELECT user.username, user.password, company.email_domain, company.email_capacity FROM sys_user user LEFT JOIN company ON user.company_id = company.id) AS a WHERE CONCAT(a.username, '@', a.email_domain) = #{username}")
    CustomUser selectByName(@Param("username") String username);

    /**
     * 统计用户名数量
     */
    @Select("SELECT count(*) FROM (SELECT user.username, user.password, company.email_domain, company.email_capacity FROM sys_user user LEFT JOIN company ON user.company_id = company.id) AS a WHERE CONCAT(a.username, '@', a.email_domain) = #{username}")
    int selectCountByName(@Param("username") String username);

    /**
     * 统计所有用户数量
     */
    @Select("SELECT count(*) FROM sys_user")
    int selectCount();

    /**
     * 获取所有用户名列表
     */
    @Select("SELECT CONCAT(a.username, '@', a.email_domain) as username FROM (SELECT user.username, user.password, company.email_domain, company.email_capacity FROM sys_user user LEFT JOIN company ON user.company_id = company.id) AS a")
    List<String> selectAll();
}
